import { updateActivity } from "../../../utils/request/api";
import msg from "../../../utils/msg";
import { Throttle } from '../../../utils/util';
const throttle = Throttle();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addContent: {
      a_id: 0 as number,
      title: '' as string,
      place: '' as string,
      start_time: '' as string,
      end_time: '' as string,
      people: '' as string,
      content: '' as string,
      limit: 0 as number,
    },
    showtime1: false,
    showtime2: false,
    minHour: 0,
    maxHour: 24,
    currentTime: new Date().getTime(),
  },
  //提交
  submit: async function () {
    if (!this.checkData()) return;
    msg.Loading('正在修改');
    await throttle(2000)
      .then(async () => {
        console.log('修改');
        await updateActivity(this.data.addContent)
          .then(async () => {
            msg.hideLoading();
            await msg.show('修改成功', '');
            wx.navigateBack();
          })
          .catch(() => {
            msg.show('修改失败', '系统维护中');
          })
        msg.hideLoading();
      })
      .catch(() => {
        // 触发节流
        msg.show('请勿重复点击', '');
      })
  },
  //检查数据不能为空
  checkData() {
    let addContent: any = this.data.addContent;
    console.log(addContent)
    for (let key of Object.keys(addContent)) {
      if (!addContent[key]) {
        msg.show('有信息未填写', '请填写完整表单后再提交');
        return false;
      }
    };
    return true;
  },
  //获取输入
  getInput(e: any) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      ['addContent.' + index]: e.detail.value,
    });
  },
  //获取开始时间
  getInputStart(event: any) {
    let addContent = this.data.addContent;
    addContent.start_time = event.detail;
    console.log(addContent.start_time);
    this.setData({
      addContent: addContent,
      showtime1: false,
    });
  },

  //获取结束时间
  getInputEnd(event: any) {
    let addContent = this.data.addContent;
    addContent.end_time = event.detail;
    this.setData({
      addContent: addContent,
      showtime2: false,
    });
  },

  //关闭弹窗
  onClose() {
    this.setData({
      showtime1: false,
      showtime2: false,
    })
  },

  //选择开始时间弹窗
  onChooseTime1() {
    this.setData({
      showtime1: true,
    })
  },

  //选择结束时间弹窗
  onChooseTime2() {
    this.setData({
      showtime2: true,
    })
  },

  //返回
  onClickLeft() {
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options: any) {
    let data = options.data || "";
    let details = JSON.parse(data);
    let addContent=this.data.addContent;
    addContent.a_id=details.id;
    addContent.title=details.title;
    addContent.people=details.people;
    addContent.place=details.place;
    addContent.start_time=details.start_time;
    addContent.end_time=details.end_time;
    addContent.content=details.content;
    addContent.limit=details.limit;
    this.setData({
      addContent: addContent,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})